﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: V_MQ400Main.cs
// 作 者：代码自动生成
// 日 期：2013/10/14 15:54:36
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;

namespace ZX.SYS.Library
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    [Table(Name = "V_MQ400Main")]
    [ZX.DBModule.Table("V_MQ400Main", "mq4id")]
    public class V_MQ400Main : ZX.DBModule.BaseEntity
    {
        #region Business Methods 业务方法
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> mq4idProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<Guid>("mq4id"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid mq4id
        {
            get { return GetProperty(mq4idProperty); }
            set { SetProperty(mq4idProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> comeintimeProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<DateTime>("comeintime"));
        [Column]
        public DateTime comeintime
        {
            get { return GetProperty(comeintimeProperty); }
            set { SetProperty(comeintimeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> comenameProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("comename"));
        [Column]
        public string comename
        {
            get { return GetProperty(comenameProperty); }
            set { SetProperty(comenameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> comeroleProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("comerole"));
        [Column]
        public string comerole
        {
            get { return GetProperty(comeroleProperty); }
            set { SetProperty(comeroleProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> companyProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("company"));
        [Column]
        public string company
        {
            get { return GetProperty(companyProperty); }
            set { SetProperty(companyProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> cphoneProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("cphone"));
        [Column]
        public string cphone
        {
            get { return GetProperty(cphoneProperty); }
            set { SetProperty(cphoneProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> cfromProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("cfrom"));
        [Column]
        public string cfrom
        {
            get { return GetProperty(cfromProperty); }
            set { SetProperty(cfromProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> crequeryProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("crequery"));
        [Column]
        public string crequery
        {
            get { return GetProperty(crequeryProperty); }
            set { SetProperty(crequeryProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> recnameProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("recname"));
        [Column]
        public string recname
        {
            get { return GetProperty(recnameProperty); }
            set { SetProperty(recnameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> addtimeProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<DateTime>("addtime"));
        [Column]
        public DateTime addtime
        {
            get { return GetProperty(addtimeProperty); }
            set { SetProperty(addtimeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> adduserProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<Guid>("adduser"));
        [Column]
        public Guid adduser
        {
            get { return GetProperty(adduserProperty); }
            set { SetProperty(adduserProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> procdeptidProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<Guid>("procdeptid"));
        [Column]
        public Guid procdeptid
        {
            get { return GetProperty(procdeptidProperty); }
            set { SetProperty(procdeptidProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> procdeptProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("procdept"));
        [Column]
        public string procdept
        {
            get { return GetProperty(procdeptProperty); }
            set { SetProperty(procdeptProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> procidProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<Guid>("procid"));
        [Column]
        public Guid procid
        {
            get { return GetProperty(procidProperty); }
            set { SetProperty(procidProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> procnameProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("procname"));
        [Column]
        public string procname
        {
            get { return GetProperty(procnameProperty); }
            set { SetProperty(procnameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> procdateProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("procdate"));
        [Column]
        public string procdate
        {
            get { return GetProperty(procdateProperty); }
            set { SetProperty(procdateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> stepresultProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("stepresult"));
        [Column]
        public string stepresult
        {
            get { return GetProperty(stepresultProperty); }
            set { SetProperty(stepresultProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> overresultProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("overresult"));
        [Column]
        public string overresult
        {
            get { return GetProperty(overresultProperty); }
            set { SetProperty(overresultProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> typeidProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("typeid"));
        [Column]
        public string typeid
        {
            get { return GetProperty(typeidProperty); }
            set { SetProperty(typeidProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> typenameProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("typename"));
        [Column]
        public string typename
        {
            get { return GetProperty(typenameProperty); }
            set { SetProperty(typenameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> procphoneProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("procphone"));
        [Column]
        public string procphone
        {
            get { return GetProperty(procphoneProperty); }
            set { SetProperty(procphoneProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> creqtypeProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("creqtype"));
        [Column]
        public string creqtype
        {
            get { return GetProperty(creqtypeProperty); }
            set { SetProperty(creqtypeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> UNameProperty = RegisterProperty(typeof(V_MQ400Main), new PropertyInfo<string>("UName"));
        [Column]
        public string UName
        {
            get { return GetProperty(UNameProperty); }
            set { SetProperty(UNameProperty, value); }
        }
        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "comename");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("comename", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "comerole");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("comerole", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "company");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("company", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "cphone");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("cphone", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "cfrom");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("cfrom", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "crequery");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("crequery", 1000));
            ValidationRules.AddRule(CommonRules.StringRequired, "recname");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("recname", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "procdept");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("procdept", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "procname");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("procname", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "procdate");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("procdate", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "stepresult");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("stepresult", 1000));
            ValidationRules.AddRule(CommonRules.StringRequired, "overresult");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("overresult", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "typeid");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("typeid", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "typename");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("typename", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "procphone");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("procphone", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "creqtype");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("creqtype", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "UName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("UName", 30));
            //base.AddBusinessRules();
        }
        #endregion

        #region Factory Methods 工厂方法

        public V_MQ400Main() { }

        public static V_MQ400Main New()
        {
            return DataPortal.Create<V_MQ400Main>();
        }
        public static V_MQ400Main Get(Guid mq4id)
        {
            return DataPortal.Fetch<V_MQ400Main>(new SingleLazyCriteria<V_MQ400Main, Guid>(mq4id, false));
        }
        public static void Delete(Guid mq4id)
        {
            DataPortal.Delete(mq4id);
        }

        #endregion

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid mq4id)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("mq4id").Equal(mq4id);

                var data = db.Select<V_MQ400Main>(q)[0];
                LoadProperty(mq4idProperty, data.mq4id);
                LoadProperty(comeintimeProperty, data.comeintime);
                LoadProperty(comenameProperty, data.comename);
                LoadProperty(comeroleProperty, data.comerole);
                LoadProperty(companyProperty, data.company);
                LoadProperty(cphoneProperty, data.cphone);
                LoadProperty(cfromProperty, data.cfrom);
                LoadProperty(crequeryProperty, data.crequery);
                LoadProperty(recnameProperty, data.recname);
                LoadProperty(addtimeProperty, data.addtime);
                LoadProperty(adduserProperty, data.adduser);
                LoadProperty(procdeptidProperty, data.procdeptid);
                LoadProperty(procdeptProperty, data.procdept);
                LoadProperty(procidProperty, data.procid);
                LoadProperty(procnameProperty, data.procname);
                LoadProperty(procdateProperty, data.procdate);
                LoadProperty(stepresultProperty, data.stepresult);
                LoadProperty(overresultProperty, data.overresult);
                LoadProperty(typeidProperty, data.typeid);
                LoadProperty(typenameProperty, data.typename);
                LoadProperty(procphoneProperty, data.procphone);
                LoadProperty(creqtypeProperty, data.creqtype);
                LoadProperty(UNameProperty, data.UName);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(mq4idProperty);
        }
        #endregion
    }
}

